//
//  VisualEffectViewController.m
//  模糊效果
//
//  Created by yq on 2022/6/16.
//

#import "VisualEffectViewController.h"

@interface VisualEffectViewController ()

@end

@implementation VisualEffectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"VisualEffect";
    
    UIScrollView *sv = [[UIScrollView alloc] initWithFrame:CGRectMake(50, 100, 300, 300)];
    UIImage *image = [UIImage imageNamed:@"cat.jpg"];
    UIImageView *iv = [[UIImageView alloc] initWithImage:image];
    sv.contentSize = iv.image.size;
    [sv addSubview:iv];
    
    [self.view addSubview:sv];
    
//    UIBlurEffect
    
    UIScrollView *sv2 = [[UIScrollView alloc] initWithFrame:CGRectMake(50, 420, 300, 300)];
    UIImage *image2 = [UIImage imageNamed:@"cat.jpg"];
    UIImageView *iv2 = [[UIImageView alloc] initWithImage:image2];
    sv2.contentSize = iv2.image.size;
    [sv2 addSubview:iv2];
    
    [self.view addSubview:sv2];
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        //1.创建模糊View
        UIBlurEffect *blur = [UIBlurEffect effectWithStyle:(UIBlurEffectStyleDark)];
        UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blur];
        //2.设定尺寸
        effectView.frame = CGRectMake(50, 150, 300, 150);
        //3.添加到view中
        [self.view addSubview:effectView];
        
        UILabel *label = [[UILabel alloc] initWithFrame:effectView.bounds];
        label.font = [UIFont systemFontOfSize:40.0f];
        label.text = @"极客学院";
        label.textColor = [UIColor redColor];
        label.textAlignment = NSTextAlignmentCenter;
//        [effectView.contentView addSubview:label];
        
        //子视图label并没有模糊
        UIVibrancyEffect *brancy = [UIVibrancyEffect effectForBlurEffect:(UIBlurEffect *)effectView.effect];
        UIVisualEffectView *subEffectView = [[UIVisualEffectView alloc] initWithEffect:brancy];
        subEffectView.frame = effectView.bounds;
        [effectView.contentView addSubview:subEffectView];
    
        [subEffectView.contentView addSubview:label];
    });
    
    static dispatch_once_t onceToken2;
    dispatch_once(&onceToken2, ^{
        //1.创建模糊View
        UIBlurEffect *blur = [UIBlurEffect effectWithStyle:(UIBlurEffectStyleDark)];
        UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blur];
        //2.设定尺寸
        effectView.frame = CGRectMake(50, 460, 300, 150);
        //3.添加到view中
        [self.view addSubview:effectView];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 300, 150)];
        label.font = [UIFont systemFontOfSize:40.0f];
        label.text = @"极客学院";
        label.textColor = [UIColor redColor];
        label.textAlignment = NSTextAlignmentCenter;
        [effectView.contentView addSubview:label];
        
        //子视图label并没有模糊
//        UIVibrancyEffect *brancy = [UIVibrancyEffect effectForBlurEffect:(UIBlurEffect *)effectView.effect];
//        UIVisualEffectView *subEffectView = [[UIVisualEffectView alloc] initWithEffect:brancy];
//        subEffectView.frame = effectView.bounds;
//        [effectView.contentView addSubview:subEffectView];
//
//        [subEffectView.contentView addSubview:label];
    });
    
}




@end
